package com.qfhd.soft.cjzcore.UI.Custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qfhd.soft.cjzcore.R;
import com.qfhd.soft.cjzcore.UI.Listener.CJClickListener;

import java.util.ArrayList;

public class CJSignView extends RelativeLayout {
    private Context mContext;
    private AttributeSet mAttr;

    private ImageView redbag_1;
    private ImageView redbag_2;
    private ImageView redbag_3;
    private ImageView redbag_4;
    private ImageView redbag_5;
    private ImageView redbag_6;
    private ImageView redbag_7;

    private TextView tx_days_1;
    private TextView tx_days_2;
    private TextView tx_days_3;
    private TextView tx_days_4;
    private TextView tx_days_5;
    private TextView tx_days_6;
    private TextView tx_days_7;

    private TextView tx_sign_btn;
    private TextView tx_sign_tip;

    private ArrayList<ImageView> img_array = new ArrayList<>();
    private ArrayList<TextView> day_array = new ArrayList<>();

    public CJSignView(Context context) {
        this(context, null);
    }

    public CJSignView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CJSignView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attr) {
        mContext = context;
        mAttr = attr;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.sign_layout, this, true);
        redbag_1 = mView.findViewById(R.id.sign_redbag_1);
        redbag_2 = mView.findViewById(R.id.sign_redbag_2);
        redbag_3 = mView.findViewById(R.id.sign_redbag_3);
        redbag_4 = mView.findViewById(R.id.sign_redbag_4);
        redbag_5 = mView.findViewById(R.id.sign_redbag_5);
        redbag_6 = mView.findViewById(R.id.sign_redbag_6);
        redbag_7 = mView.findViewById(R.id.sign_redbag_7);

        tx_days_1 = mView.findViewById(R.id.sign_days_1);
        tx_days_2 = mView.findViewById(R.id.sign_days_2);
        tx_days_3 = mView.findViewById(R.id.sign_days_3);
        tx_days_4 = mView.findViewById(R.id.sign_days_4);
        tx_days_5 = mView.findViewById(R.id.sign_days_5);
        tx_days_6 = mView.findViewById(R.id.sign_days_6);
        tx_days_7 = mView.findViewById(R.id.sign_days_7);

        tx_sign_btn = mView.findViewById(R.id.sign_signTxt);
        tx_sign_tip = mView.findViewById(R.id.sign_tip);

        img_array.add(redbag_1);
        img_array.add(redbag_2);
        img_array.add(redbag_3);
        img_array.add(redbag_4);
        img_array.add(redbag_5);
        img_array.add(redbag_6);
        img_array.add(redbag_7);

        day_array.add(tx_days_1);
        day_array.add(tx_days_2);
        day_array.add(tx_days_3);
        day_array.add(tx_days_4);
        day_array.add(tx_days_5);
        day_array.add(tx_days_6);
        day_array.add(tx_days_7);
    }

    public void setSignLength(int signLength){
        //第几个 7
        //红包 显示
        //第几天

        int count = 0;
        int index = 0;
        if(signLength == 0) count = 0;
        else count = signLength / 7;

        if(signLength == 0) index = 0;
        else {
            index = signLength % 7;
            if(index == 0) index = 7;
        }

        showDays(count,index);
        showRedbag(index);
        showTip(signLength);
    }

    private void showDays(int count,int index){
        for(int i=0; i<day_array.size(); i++){
            TextView tx_day = day_array.get(i);
            if(index != 7){
                if(i <= index-1){
                    tx_day.setText("已领取");
                }
                else{
                    tx_day.setText("第" + (count*7+i+1) + "天");
                }
            }
            else{
                if(count == 0){
                    int tmp =  i + 1;
                    if(i <= index-1){
                        tx_day.setText("已领取");
                    }
                    else {
                        tx_day.setText("第" + tmp + "天");
                    }
                }
                else{
                    int tmp = (count-1) * 7 + i + 1;
                    if(i <= index-1){
                        tx_day.setText("已领取");
                    }
                    else{
                        tx_day.setText("第" + tmp + "天");
                    }

                }

            }
        }
    }

    private void showRedbag(int index){
        for(int i=0; i<img_array.size(); i++){
            ImageView img = img_array.get(i);
            if( i <= index-1){
                img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.redbag_able));
            }
            else{
                img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.redbag_enable));
            }
        }
    }

    private void showTip(int signLegth){
        tx_sign_tip.setText("已经连续签到"+ signLegth +"天");
    }

    public void setSignBtnListener(CJClickListener listener){
        tx_sign_btn.setOnClickListener(v -> {
            if(listener != null){
                listener.onClick();
            }
        });
    }

    public void beginSign(){
        tx_sign_btn.setEnabled(false);
        tx_sign_btn.setText("签到中，请稍等");
    }

    public void signSuccess(int signLength){
        tx_sign_btn.setText("已签到");
        tx_sign_btn.setEnabled(false);
        tx_sign_btn.setBackground(mContext.getResources().getDrawable(R.drawable.radius_10_gray));
        setSignLength(signLength);
    }

    public void signFailed(){
        tx_sign_btn.setText("签到");
        tx_sign_btn.setEnabled(true);
        tx_sign_btn.setBackground(mContext.getResources().getDrawable(R.drawable.radius_10_theme));
    }

    public void hadSigned(boolean isSigned){
        if(isSigned) {
            tx_sign_btn.setText("已签到");
            tx_sign_btn.setEnabled(false);
            tx_sign_btn.setBackground(mContext.getResources().getDrawable(R.drawable.radius_10_gray));
        }
        else{
            signFailed();
        }
    }
}
